﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TinyPMS_Jerusalem.Models.DBModel;
using TinyPMS_Jerusalem.Models;


//Todo:需要写一个HTMLHelper来显示List<T>，功能如同Html.DisplayFor()


namespace TinyPMS_Jerusalem.Controllers
{
    [HandleError(View="Error")]
    public class PropertyController : Controller
    {
        //创建数据模型类的实例
        Models.PropertyDBModel propertyDBModel = new PropertyDBModel(new DBModelDataContext());

        //
        // GET: /Property/
        public ActionResult List(string id)
        {
            List<Property> propertyList = propertyDBModel.GetAll(g => g.ID == id).ToList();
            if (propertyList.Count() == 0)
            {
                throw new ArgumentException("未找到对应的物品。");
            }
            else
            {
                return View(propertyList.Single());//返回单个物品
            }
        }

        //
        // GET: /Property/ListAll
        public ActionResult ListAll()
        {
            List<Property> propertyList = propertyDBModel.GetAll().ToList();
            if (propertyList.Count() == 0)
            {
                throw new ArgumentException("未找到任何物品。");
            }
            else
            {
                return View(propertyList);
            }
        }

        //
        // GET: /Property/Details/5
        public ActionResult Details(string id)
        {
            PropertyDetailDBModel db = new PropertyDetailDBModel();
            if (!propertyDBModel.IsExist(id))
                throw new ArgumentException("未找到对应的物品");
            ViewData.Add("Result", db.GetDetail(id));   //将对应的Detail对象添加到ViewData["Result"]里
            if (ViewData["Result"] == null)
                throw new ArgumentException("未找到对应对象的详细信息");
            return View();
        }


        /// <summary>
        /// 用于验证物品ID是否已经存在
        /// </summary>
        /// <param name="id">需要检查的ID</param>
        /// <returns>如果ID已存在则返回false，不存在则返回true</returns>
        [NonAction()]
        // GET: /Property/IsUnique/5
        public bool IsUnique(string id)
        {
            return !(propertyDBModel.IsExist(id));
        }

        // GET: /Property/Borrow/5
        [Authorize()]
        public ActionResult Borrow(string id)
        {
            if (propertyDBModel.IsExist(id) == false)
            {
                throw new ArgumentException("未找到对应的物品。");
            }
            else
            {
                propertyDBModel.Borrow(propertyDBModel.GetElementByID(id), User.Identity.Name);
                return RedirectToAction("Index", "Home");
            }
        }


        public ActionResult Search(string keywords)
        {
            List<Property> result=propertyDBModel.Search(keywords);
            return View(result);
        }

        [Authorize()]
        public ActionResult Return(string id)
        {
            List<Property> propertyList = propertyDBModel.GetAll(c => c.ID == id).ToList();
            if (propertyList.Count <= 0)
                throw new ArgumentException("未找到对应的物品");
            else
            {
                Property property = propertyList.Single();
                if (property.Status == false)
                    throw new Exceptions.PermissionDeniedException("您无法归还当前物品");
                else
                {
                    if (property.BorrowerName != User.Identity.Name)
                        throw new Exceptions.PermissionDeniedException("您无法归还不是您借出的物品");
                    else
                    {
                        propertyDBModel.Return(property, User.Identity.Name);
                        propertyDBModel.Save();
                        return RedirectToAction("MyBorrowings");
                    }
                }
            }
        }

        [Authorize()]
        public ActionResult MyBorrowings()
        {
            List<Property> propertyList=propertyDBModel.GetAll(c => c.BorrowerName == User.Identity.Name).ToList();
            return View(propertyList);
        }

        [Authorize()]
        public ActionResult Renew(string id)
        {
            List<Property> propertyList=propertyDBModel.GetAll(c => c.ID == id).ToList();
            if (propertyList.Count <= 0)
                throw new ArgumentException("对应的物品未找到");
            else
            {
                Property property = propertyList.Single();
                if (property.Status == false || property.BorrowerName.ToLower() != User.Identity.Name.ToLower())
                {
                    throw new Exceptions.PermissionDeniedException("您无法续借该物品");
                }
                else
                {
                    if (property.RenewCount >= property.MaxRenewCount)
                        throw new Exceptions.PermissionDeniedException("已超过最大续借次数，您无法续借该物品");
                    else
                    {
                        propertyDBModel.Renew(propertyDBModel.GetElementByID(id), User.Identity.Name.ToLower());
                        property.RenewCount++;
                        propertyDBModel.Save();
                        return RedirectToAction("MyBorrowings");
                    }
                }
            }
        }

    }
}
